package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.service.IAdmMqService;
import com.dss.auto_deploy.dto.*;
import com.dss.auto_deploy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
* mq消息队列
* @Description:mq消息队列控制层
* @author dongshoushan
* @date 2022-11-03 19:24:21
*/
@RestController
@RequestMapping("/admMq")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: mq消息队列")
public class AdmMqController {

    /**
    * mq消息队列Service
    */
    private final  IAdmMqService admMqService;
    private final HttpServletRequest request;

    /**
    * 新增mq消息队列
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增mq消息队列", notes="新增mq消息队列")
    public Result insert(@RequestBody @Valid AdmMqAddDto addDto) {
        admMqService.insert(addDto);
        return Result.success("新增成功");
    }

    /**
    * 修改mq消息队列
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改mq消息队列", notes="修改mq消息队列")
    public Result update(@RequestBody @Valid AdmMqUpdateDto updateDto) {
        admMqService.update(updateDto);
        return Result.success("修改成功");
    }

    /**
    * 批量删除mq消息队列
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除mq消息队列", notes="删除mq消息队列")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admMqService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
    * 分页查询mq消息队列
    * @param pageDto
    * @return
    */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询mq消息队列", notes="分页查询mq消息队列")
    public Result page(AdmMqPageDto pageDto) throws InterruptedException {
        return Result.success(admMqService.list(pageDto));
    }

    @ApiOperation(value = "rabbitmq安装", notes="rabbitmq安装")
    @PostMapping(value = "installRabbitmq", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result installrabbitmq(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestPart(value = "rabbitmqFile") MultipartFile rabbitmqFile) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admMqService.installRabbitmq(ids,tempPath,rabbitmqFile);
        return Result.success();
    }
}
